<?php
/**
 * Fields are the components that documents are made up of.
 * Each field consists of a name, a value and zero or more
 * properties.
 *
 * @author Rob Young <rob@roryoung.co.uk>
 * @package forage.document
 */
class ForageField
{
  /**
   * @var string
   */
  private $name;

  /**
   * @var string
   */
  private $value;

  /**
   * Configuration properties relating to this field.
   *
   * @var array
   */
  private $properties = array();

  /**
   * Class constructor.
   * Create a ForageField with a name and value set.
   *
   * @param string $name
   * @param string $value
   */
  public function __construct($name, $value)
  {
    $this->name = $name;
    $this->value = $value;
  }

  /**
   * Magic accessor
   *
   * @param string $name
   * @return mixed
   */
  public function __get($name)
  {
    switch ($name) {
    case 'name':
    case 'value':
    case 'properties':
      return $this->$name;
    default:
      throw new InvalidArgumentException("Invalid property access [" . $name . "]");
    }
  }

  public function __set($name, $value)
  {
    switch ($name) {
    case 'value':
      $this->$name = $value;
      break;
    default:
      throw new InvalidArgumentException("Invalid property setting [" . $name . "]");
    }
  }

  /**
   * Set a configuration property.
   *
   * @param string $name
   * @param string $value
   */
  public function setProperty($name, $value)
  {
    $this->properties[$name] = $value;
  }

  /**
   * Get a previously set configuration property
   *
   * @see setProperty
   * @param string $name
   * @param string $default return this if the property isn't set
   * @return string
   */
  public function getProperty($name, $default=null)
  {
    return isset($this->properties[$name]) ? $this->properties[$name] : $default;
  }


  /**
   * Return a string representation of this object.
   *
   * @return string
   */
  public function __toString()
  {
    return $this->value;
  }
}
